package com.yangxk.kadmin.web.module.sample.controller;

import com.yangxk.kadmin.web.module.sample.form.BookEditForm;
import com.yangxk.kadmin.web.module.sample.form.BookQueryForm;
import com.yangxk.kadmin.web.module.sample.service.BookService;
import com.yangxk.kadmin.web.module.sample.po.Book;
import com.yangxk.kadmin.common.beans.Result;
import com.yangxk.kadmin.web.common.annotation.Permission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class BookController {
    @Autowired
    private BookService bookService;


    @GetMapping("/book/list")
    public Result<PageInfo> queryByPage(BookQueryForm bookQueryForm) {
        return Result.ofSuccess(bookService.queryByPage(bookQueryForm));
    }

    @PostMapping("/book/add")
    @Permission("book:add")
    public Result add(@RequestBody BookEditForm form) {
        bookService.addBook(form);
        return Result.ofSuccess();
    }

    @GetMapping("/book/detail")
    public Result<Book> queryById(long id) {
        Book book = bookService.selectByPrimaryKey(id);
        return Result.ofSuccess(book);
    }

    @PostMapping("/book/update")
    @Permission("book:update")
    public Result update(@RequestBody BookEditForm form) {
        bookService.updateBook(form);
        return Result.ofSuccess();
    }

    @GetMapping("/book/delete")
    @Permission("book:delete")
    public Result delete(long id) {
        bookService.deleteBook(id);
        return Result.ofSuccess();
    }

}
